#include "SFOSInformation.h"


SFOSInformation::SFOSInformation( void )
{
}


SFOSInformation::~SFOSInformation( void )
{
}


bool SFOSInformation::CheckOS( void )
{
	OSVERSIONINFOEX osVersionInfoEx;
	SYSTEM_INFO systemInfo;
	PGNSI pGNSI;
	PGPI pGPI;
	unsigned long ulType;


	memset( &osVersionInfoEx, 0, sizeof( osVersionInfoEx ) );
	memset( &systemInfo, 0, sizeof( systemInfo ) );	
	osVersionInfoEx.dwOSVersionInfoSize = sizeof( OSVERSIONINFOEX );


	if( GetVersionEx( (OSVERSIONINFO*)&osVersionInfoEx ) )
	{
		if( pGNSI = (PGNSI)GetProcAddress( GetModuleHandle( "kernel32.dll" ), "GetNativeSystemInfo" ) )
		{
			pGNSI( &systemInfo );
		}
		else
		{
			GetSystemInfo( &systemInfo );
		}


		if( VER_PLATFORM_WIN32_NT == osVersionInfoEx.dwPlatformId  &&  4 < osVersionInfoEx.dwMajorVersion )
		{
			m_strOSInfo.append( "Microsoft " );

			if( 6 == osVersionInfoEx.dwMajorVersion )
			{
				switch( osVersionInfoEx.dwMinorVersion )
				{
				case 0 :
					{
						switch( osVersionInfoEx.wProductType )
						{
						case VER_NT_WORKSTATION :
							m_strOSInfo.append( "Windows Vista " );
							break;

						case VER_NT_DOMAIN_CONTROLLER :
						case VER_NT_SERVER :
							m_strOSInfo.append( "Windows Server 2008 " );
							break;
						}
					}
					break;

				case 1 :
					{
						switch( osVersionInfoEx.wProductType )
						{
						case VER_NT_WORKSTATION :
							m_strOSInfo.append( "Windows 7 " );
							break;

						case VER_NT_DOMAIN_CONTROLLER :
						case VER_NT_SERVER :
							m_strOSInfo.append( "Windows Server 2008 R2 " );
							break;
						}
					}
					break;
				}


				pGPI = (PGPI)GetProcAddress( GetModuleHandle( "kernel32.dll" ), "GetProductInfo" );
				pGPI( osVersionInfoEx.dwMajorVersion, osVersionInfoEx.dwMinorVersion, 0, 0, &ulType );

				switch( ulType )
				{
				case PRODUCT_ULTIMATE:
					m_strOSInfo.append( "Ultimate Edition" );
					break;
				case PRODUCT_PROFESSIONAL:
					m_strOSInfo.append( "Professional" );
					break;
				case PRODUCT_HOME_PREMIUM:
					m_strOSInfo.append( "Home Premium Edition" );
					break;
				case PRODUCT_HOME_BASIC:
					m_strOSInfo.append( "Home Basic Edition" );
					break;
				case PRODUCT_ENTERPRISE:
					m_strOSInfo.append( "Enterprise Edition" );
					break;
				case PRODUCT_BUSINESS:
					m_strOSInfo.append( "Business Edition" );
					break;
				case PRODUCT_STARTER:
					m_strOSInfo.append( "Starter Edition" );
					break;
				case PRODUCT_CLUSTER_SERVER:
					m_strOSInfo.append( "Cluster Server Edition" );
					break;
				case PRODUCT_DATACENTER_SERVER:
					m_strOSInfo.append( "Datacenter Edition" );
					break;
				case PRODUCT_DATACENTER_SERVER_CORE:
					m_strOSInfo.append( "Datacenter Edition (core installation)" );
					break;
				case PRODUCT_ENTERPRISE_SERVER:
					m_strOSInfo.append( "Enterprise Edition" );
					break;
				case PRODUCT_ENTERPRISE_SERVER_CORE:
					m_strOSInfo.append( "Enterprise Edition (core installation)" );
					break;
				case PRODUCT_ENTERPRISE_SERVER_IA64:
					m_strOSInfo.append( "Enterprise Edition for Itanium-based Systems" );
					break;
				case PRODUCT_SMALLBUSINESS_SERVER:
					m_strOSInfo.append( "Small Business Server" );
					break;
				case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
					m_strOSInfo.append( "Small Business Server Premium Edition" );
					break;
				case PRODUCT_STANDARD_SERVER:
					m_strOSInfo.append( "Standard Edition" );
					break;
				case PRODUCT_STANDARD_SERVER_CORE:
					m_strOSInfo.append( "Standard Edition (core installation)" );
					break;
				case PRODUCT_WEB_SERVER:
					m_strOSInfo.append( "Web Server Edition" );
					break;
				}
			}
		}


		if( osVersionInfoEx.dwMajorVersion == 5 && osVersionInfoEx.dwMinorVersion == 2 )
		{
			if( GetSystemMetrics( SM_SERVERR2 ) )
			{
				m_strOSInfo.append( "Windows Server 2003 R2, " );
			}
			else if( osVersionInfoEx.wSuiteMask & VER_SUITE_STORAGE_SERVER )
			{
				m_strOSInfo.append( "Windows Storage Server 2003" );
			}
			else if( osVersionInfoEx.wSuiteMask & VER_SUITE_WH_SERVER )
			{
				m_strOSInfo.append( "Windows Home Server" );
			}
			else if( osVersionInfoEx.wProductType == VER_NT_WORKSTATION && systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 )
			{
				m_strOSInfo.append( "Windows XP Professional x64 Edition" );
			}
			else
			{
				m_strOSInfo.append( "Windows Server 2003, " );
			}


			if( osVersionInfoEx.wProductType != VER_NT_WORKSTATION )
			{
				if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
				{
					if( osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER )
					{
						m_strOSInfo.append( "Datacenter Edition for Itanium-based Systems" );
					}
					else if( osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE )
					{
						m_strOSInfo.append( "Enterprise Edition for Itanium-based Systems" );
					}
				}
			}
            else if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 )
            {
				if( osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER )
				{
					m_strOSInfo.append( "Datacenter x64 Edition" );
				}
				else if( osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE )
				{
					m_strOSInfo.append( "Enterprise x64 Edition" );
				}
                else
				{
					m_strOSInfo.append( "Standard x64 Edition" );
				}
            }
            else
            {
                if( osVersionInfoEx.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
				{
					m_strOSInfo.append( "Compute Cluster Edition" );
				}
                else if( osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER )
				{
					m_strOSInfo.append( "Datacenter Edition" );
				}
                else if( osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE )
				{
					m_strOSInfo.append( "Enterprise Edition" );
				}
                else if( osVersionInfoEx.wSuiteMask & VER_SUITE_BLADE )
				{
					m_strOSInfo.append( "Web Edition" );
				}
                else
				{
					m_strOSInfo.append( "Standard Edition" );
				}
            }
        }

		if( osVersionInfoEx.dwMajorVersion == 5  &&  osVersionInfoEx.dwMinorVersion == 1 )
		{
			m_strOSInfo.append( "Windows XP ");

			if( osVersionInfoEx.wSuiteMask & VER_SUITE_PERSONAL )
			{
				m_strOSInfo.append( "Home Edition" );
			}
			else
			{
				m_strOSInfo.append( "Professional" );
			}
		}


		if( osVersionInfoEx.dwMajorVersion == 5 && osVersionInfoEx.dwMinorVersion == 0 )
		{
			m_strOSInfo.append( "Windows 2000 ");

			if( osVersionInfoEx.wProductType == VER_NT_WORKSTATION )
			{
				m_strOSInfo.append( "Professional" );
			}
			else 
			{
				if( osVersionInfoEx.wSuiteMask & VER_SUITE_DATACENTER )
				{
					m_strOSInfo.append( "Datacenter Server" );
				}
				else if( osVersionInfoEx.wSuiteMask & VER_SUITE_ENTERPRISE )
				{
					m_strOSInfo.append( "Advanced Server" );
				}
				else
				{
					m_strOSInfo.append( "Server" );
				}
			}
		}



		if( strlen( osVersionInfoEx.szCSDVersion ) > 0 )
		{
			m_strOSInfo.append( " " );
			m_strOSInfo.append( osVersionInfoEx.szCSDVersion );
		}


		char temp[80];
		memset( temp, 0, sizeof( temp ) );
		_itoa_s( osVersionInfoEx.dwBuildNumber, temp, 80, 10 );

		m_strOSInfo.append( " (build " );
		m_strOSInfo.append( temp );
		m_strOSInfo.append( ")" );


		if( osVersionInfoEx.dwMajorVersion >= 6 )
		{
			if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 )
			{
				m_strOSInfo.append( ", 64-bit" );
			}
			else if( systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL )
			{
				m_strOSInfo.append( ", 32-bit" );
			}
		}
      
		return true;
	}


	m_strOSInfo.append( "This sample does not support this version of Windows." );


	return false;
}